﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;
using Microsoft.TeamFoundation.Client;
using Microsoft.VisualStudio.TeamFoundation;
using PowersourceUIToolkit;
using UCL.Powersource.Analysis;

namespace UCL.Powersource.TFS
{
    /// <summary>
    /// Collection of tfs stat project collection configuration
    /// </summary>
    [DataContract]
    [KnownType(typeof(AnalysisBridge))]
    [KnownType(typeof(TfsTeamProjectCollectionConfiguration))]
    [KnownType(typeof(TfsTeamProjectConfiguration))]
    [KnownType(typeof(TimeFrame))]
    [KnownType(typeof(TimeFrameIteration))]
    [KnownType(typeof(StatisticCollectionWithResults))]
    public class PackageConfiguration : IPackageConfiguration
    {
        public readonly ILog Log;

        [DataMember]
        private List<TfsTeamProjectCollectionConfiguration> _TFSTeamCollectionConfigurations;

        public IReadOnlyList<ITfsTeamProjectCollectionConfiguration> TFSTeamCollectionConfigurations
        {
            get { return _TFSTeamCollectionConfigurations; }
            //private set { _TFSTeamCollectionConfigurations = (IReadOnlyList<TfsTeamProjectCollectionConfiguration>)value; }
        }

        [DataMember]
        private StatisticCollectionWithResults _TeamProjectCollectionStatistics;

        public IStatisticCollectionWithResults TeamProjectCollectionStatistics
        {
            get { return _TeamProjectCollectionStatistics ?? (_TeamProjectCollectionStatistics = new StatisticCollectionWithResults()); }

        }

        [DataMember]
        public string DefaultIldasmPath { get; private set; }
        [DataMember]
        public string DefaultLocalDropBasePath { get; private set; }
        [DataMember]
        public string DefaultCodeIndexDBServer { get; private set; }
        [DataMember]
        public string DefaultExportPath { get; private set; }

        public PackageConfiguration()
        {
            Log = LogManager.GetLog(typeof(PackageConfiguration));
            _TFSTeamCollectionConfigurations = new List<TfsTeamProjectCollectionConfiguration>();

            string prFiles = null;
            if (Environment.GetEnvironmentVariables().Contains("ProgramFiles(x86)"))
                prFiles = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            else
                prFiles = Environment.GetEnvironmentVariable("ProgramFiles");

            DefaultIldasmPath = prFiles + "\\Microsoft SDKs\\Windows\\v8.0A\\bin\\NETFX 4.0 Tools\\ildasm.exe";
            
            DefaultLocalDropBasePath = Path.GetTempPath() + "PowersourceBuild";
            DefaultExportPath = Environment.GetEnvironmentVariable("USERPROFILE") + "\\Documents";
            DefaultCodeIndexDBServer = "(localdb)\\v11.0";
        }

        /// <summary>
        /// SynchronizeTeamsAndTimeFrames tfs stat project collection
        /// </summary>
        /// <param name="collection"></param>
        public void MergeWithCollection(ITfsTeamProjectCollection collection)
        {
            if (collection == null) { Log.Debug("MergeWithCollection called with collection==null. Ignored."); return; }
            var tcc = _TFSTeamCollectionConfigurations.SingleOrDefault(a => a.Uri == collection.Uri.ToString());
            if (tcc == null)
            {
                var collUri = collection.Uri.ToString();
                tcc = new TfsTeamProjectCollectionConfiguration(collUri) 
                { IldasmPath = DefaultIldasmPath, 
                    LocalDropBasePath = DefaultLocalDropBasePath, CodeIndexDBServer = DefaultCodeIndexDBServer, ExportPath = DefaultExportPath };
                tcc.WebServiceUrl = CreateDefaultWebServiceUrl(collUri);
                _TFSTeamCollectionConfigurations.Add(tcc);
            }
            tcc.TfsTeamProjectCollection = collection;
            tcc.Name = collection.Name;
            tcc.Synchronize();
        }

        public string CreateDefaultWebServiceUrl(string collectionUrl)
        {
            var uriBase = collectionUrl.Split(new char[] { ':' });
            return uriBase[0] + ":" + uriBase[1] + ":81/Powersource.WebApp.Test/Services/RemoteBuildBridge.svc";
        }
    }
}
